<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="/js/plugins/jrender/jrender.min.js"></script>
		<link rel="stylesheet" href="/js/plugins/dialog2/dialog.css" />
		<script src="/js/plugins/dialog2/dialog.min.js"></script>
		<script src="/js/plugins/form/jquery.form.js"></script>
		<script src="/js/plugins/validate/jquery.validate.min.js"></script>
		<script src="/js/plugins/validate/messages_cn.js"></script>

		<script>
			$(function () {
                $("#infoForm").validate({
                    rules: {
                        nickName: {
                            required:true,
                            rangelength:[2,6]
                        },
						place: {
                            required:true
                        }
                    }
                });



				var user=JSON.parse(sessionStorage.getItem("user"));
                $(".info").renderValues(user);
                $(".myBg").css("background-image","url("+user.coverImgUrl+")");
                $("#saveBtn").click(function () {
                    $("#infoForm").prop("action","/users/"+user.id);
					$("#infoForm").ajaxForm(function (data) {
                        if (data.success) {
                            $(document).dialog({
                                autoClose: 1200,
                                content: '保存成功'
                            });
							setTimeout(function () {
								location.href="/mine/profiles.html";
                            },1200)
							sessionStorage.setItem("user",JSON.stringify(data.user));
                        }else {
                            $(document).dialog({
                                titleShow: false,
                                content: date.msg
                            });
						}
                    })
                    $("#infoForm").submit();
                })
				var temp=null;
				$("#headImg").click(function () {
					$("#imageSumbit").click();
					temp ="headImgUrl"
                })
				$("#coverBtn").click(function () {
                    $("#imageSumbit").click();
					temp="cover"
                })


				$("#imageSumbit").change(function () {
                    if ($(this).val()) {
                        $("#fileForm").ajaxSubmit(function (data) {
                            if (data.status) {
                                if (temp == "headImgUrl") {
                                    $("#headImg").prop("src",data.url)
                                    $("#infoForm input[name='headImgUrl']").val(data.url);
                                }else {
                                    $(".myBg").css("background-image","url("+data.url+")");
                                    $("#infoForm input[name='coverImgUrl']").val(data.url);
                                }
                            }
                        })
                    }
                })
            })
		</script>

	</head>

	<body>

		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="#" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<div class="container info">
			<div class="myBg" >
				<img id="headImg" render-src="headImgUrl"  class="rounded-circle"/>
				<div class="editHead" id="headBtn" ><i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i></div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>
			<form id="fileForm" action="/images" method="post" enctype="multipart/form-data"/>
				<input type="file" name="file" id="imageSumbit" style="display: none" accept="image/*">
			</form>

			<form id="infoForm" method="post" >
					<input type="hidden" name="_method" value="put" >
					<input type="hidden" name="email" render-value="email">
					<input type="hidden" name="headImgUrl" render-value="headImgUrl">
					<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">

					<label>昵称</label><input render-value="nickName" type="text" name="nickName" class="form-control" >
					<label>性别</label><select class="form-control" name="gender" render-value="gender" >
							<option value="-1">保密</option>
							<option value="1">男</option>
							<option value="0">女</option>
						</select>
					<label>地区</label><input type="text" name="place" render-value="place"  class="form-control">
					<label>签名</label><textarea name="sign" class="form-control" render-value="sign" rows="5" ></textarea>
			</form>
		</div>
		
		
		
	</body>
</html>
